package com.example.farmer.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.Entity.ProductInfo;
import com.example.farmer.R;

import java.util.ArrayList;
import java.util.List;

public class RightListAdapter extends RecyclerView.Adapter<RightListAdapter.MyHolder> {
    private List<ProductInfo> mProductInfo =new ArrayList<>();


    public void setListData(List<ProductInfo> list){

        this.mProductInfo =list;

        //刷新
        notifyDataSetChanged();



    }

    private LeftListAdapter.LeftListOnClickItemListener mleftListOnClickItemListener;

    public void setMleftListOnClickItemListener(LeftListAdapter.LeftListOnClickItemListener mleftListOnClickItemListener) {
        this.mleftListOnClickItemListener = mleftListOnClickItemListener;
    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        //加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_list_item, null);
        return new MyHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {


        //绑定数据
        ProductInfo productInfo = mProductInfo.get(position);

        holder.product_img.setImageResource(productInfo.getProduct_img());
        holder.product_title.setText(productInfo.getProduct_title());
        holder.product_details.setText(productInfo.getProduct_details());
        holder.product_price.setText(productInfo.getProduct_price()+"");
        holder.product_time.setText(productInfo.getProduct_time()+"");
        holder.ll_click.setOnClickListener(v->{
            if (null!=mleftListOnClickItemListener){
                mleftListOnClickItemListener.onItemClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mProductInfo.size();
    }

    static  class MyHolder extends RecyclerView.ViewHolder{


        ImageView product_img;
        TextView product_title;
        TextView product_details;
        TextView product_price;
        TextView product_time;
        LinearLayoutCompat ll_click;


        public MyHolder(@NonNull View itemView) {
            super(itemView);
            product_img =itemView.findViewById(R.id.product_img);
            product_title =itemView.findViewById(R.id.product_title);
            product_details =itemView.findViewById(R.id.product_details);
            product_price =itemView.findViewById(R.id.product_price);
            product_time =itemView.findViewById(R.id.product_time);
            ll_click =itemView.findViewById(R.id.ll_click);

        }
    }
}
